//api获取分页信息适用
var pageControl = {
	control: '',
	action: '',
	param: {
		Page: 1,
		Size: 500,
		totalPage: 1,
		totalRecord: 0,
		needLayer: true,		//用于判断是否需要显示加载层
		alwaysCallBack: null,	//执行api以后，总是会执行的方法
	},
	//加载列表
	loadList:function(callBack){
		var $this = this;
		if($this.param.needLayer)
			layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
		Y.API2($this.control, $this.action, $this.param,function(data){
			if($this.param.needLayer)
				layer.closeAll('dialog');
			
			if(!Y.checkNull($this.param.alwaysCallBack))
				$this.param.alwaysCallBack();
			
			if(data.Success == false){
				swal(data.Message, '', 'error');
			}else{
				$this.param.totalPage = data['页面总数'];
				$('#pageCount').text($this.param.totalPage);
				$this.param.totalRecord = data['记录总数'];
				$('#recordCount').text($this.param.totalRecord);
				$('#pageNum').val($this.param.Page);

				if ($this.param.Page == 1) {
					$('.firstPage').addClass('notUse');
					$('.prevPage').addClass('notUse');
				} else {
					$('.firstPage').removeClass('notUse');
					$('.prevPage').removeClass('notUse');
				}

				if ($this.param.Page >= $this.param.totalPage) {
					$('.lastPage').addClass('notUse');
					$('.nextPage').addClass('notUse');
				} else {
					$('.lastPage').removeClass('notUse');
					$('.nextPage').removeClass('notUse');
				}
				
				callBack({
					page: $this.param.Page,
					data: data
				});
			}
		})
	},
	//绑定ui
	bindUI: function(){
		var html  = '';
			html += '<span id="recordCount">'+ this.param.totalRecord +'</span>'
			html += '<span id="pageCount">'+ this.param.totalPage +'</span>';
			html += '<input id="goToPage" class="btn btn-primary" type="button" value="GO" />';
			html += '<i class="lastPage fa fa-step-forward"></i>';
			html += '<i class="nextPage fa fa-forward"></i>';
			html += '<input id="pageNum" class="form-control" type="number" value="'+ this.param.Page +'" />';
			html += '<i class="prevPage fa fa-backward"></i>';
			html += '<i class="firstPage fa fa-step-backward"></i>';
		$('.pageControl').append(html);
	},
	//绑定事件
	bindEvants: function(callBack){
		//改变页数加载列表
		var $this = this;
		var changePage = function(pageNo,element){
			if(element.hasClass('notUse'))
				return false;
			$this.param.Page = pageNo;
			$this.loadList(callBack);
		}
		//点击首页
		$('.firstPage').click(function(){
			changePage(1, $(this));
		});
		//点击上一页
		$('.prevPage').click(function(){
			changePage($this.param.Page - 1, $(this));
		});
		//点击下一页
		$('.nextPage').click(function(){
			changePage($this.param.Page + 1, $(this));
		});
		//点击尾页
		$('.lastPage').click(function(){
			changePage($this.param.totalPage, $(this));
		});
		//跳转到指定页数
		$('#goToPage').click(function(){
			var goToPage = parseInt($('#pageNum').val());
			if(isNaN(goToPage))
				return false;
			if(goToPage > $this.param.totalPage){
				goToPage = $this.param.totalPage;
			}
			if(goToPage < 1){
				goToPage = 1;
			}
			changePage(goToPage, $(this));
		});
		$('#pageNum').keyup(function(event){   
     		//获取当前按键的键值   
     		//jQuery的event对象上有一个which的属性可以获得键盘按键的键值   
     		var keycode = event.which;   
    		//处理回车的情况   
     		if(keycode == 13){
     			$('#goToPage').click();
    		} 
 		});
	},
	//初始化组件
	init: function(control,action,param,callBack){
		//初始化参数
		this.control = control;
		this.action = action;
		this.param = $.extend({}, this.param, param);
		
		//如果组件初始化需要绑定UI
		if($('.pageControl').is(':empty')){
			this.bindUI();
			this.bindEvants(callBack);
		}
			
		//执行api
		this.loadList(callBack);
	}
}

//js逻辑操作分页适用
var pageAllControl = {
	param: {
		Page: 1,
		Size: 500,
		totalPage: 1,
		totalRecord: 0,
		needLayer: true,		//用于判断是否需要显示加载层
		alwaysCallBack: null,	//执行api以后，总是会执行的方法
		result:[],				//用于存储总数据
	},
	//加载列表
	loadList:function(callBack){
		var $this = this;
		//处理页码相关逻辑
		$('#pageNum').val($this.param.Page);
		if ($this.param.Page == 1) {
			$('.firstPage').addClass('notUse');
			$('.prevPage').addClass('notUse');
		} else {
			$('.firstPage').removeClass('notUse');
			$('.prevPage').removeClass('notUse');
		}
		if ($this.param.Page >= $this.param.totalPage) {
			$('.lastPage').addClass('notUse');
			$('.nextPage').addClass('notUse');
		} else {
			$('.lastPage').removeClass('notUse');
			$('.nextPage').removeClass('notUse');
		}
		//处理输出信息
		var returnData = [];
		$this.param.result.map(function(item,index){
			index = index + 1;
			if(index > ($this.param.Page - 1) * $this.param.Size && index < $this.param.Page * $this.param.Size + 1){
				returnData.push(item);
			}
		})
		callBack({
			page: $this.param.Page,
			data: returnData
		});
	},
	//绑定ui
	bindUI: function(){
		var html  = '';
			html += '<span id="recordCount">'+ this.param.totalRecord +'</span>'
			html += '<span id="pageCount">'+ this.param.totalPage +'</span>';
			html += '<input id="goToPage" class="btn btn-primary" type="button" value="GO" />';
			html += '<i class="lastPage fa fa-step-forward"></i>';
			html += '<i class="nextPage fa fa-forward"></i>';
			html += '<input id="pageNum" class="form-control" type="number" value="'+ this.param.Page +'" />';
			html += '<i class="prevPage fa fa-backward"></i>';
			html += '<i class="firstPage fa fa-step-backward"></i>';
		$('.pageControl').append(html);
	},
	//绑定事件
	bindEvants: function(callBack){
		//改变页数加载列表
		var $this = this;
		var changePage = function(pageNo,element){
			if(element.hasClass('notUse'))
				return false;
			layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
			$this.param.Page = pageNo;
			$this.loadList(callBack);
			setTimeout(function(){
				layer.closeAll('dialog');
			},500);
		}
		//点击首页
		$('.firstPage').click(function(){
			changePage(1, $(this));
		});
		//点击上一页
		$('.prevPage').click(function(){
			changePage($this.param.Page - 1, $(this));
		});
		//点击下一页
		$('.nextPage').click(function(){
			changePage($this.param.Page + 1, $(this));
		});
		//点击尾页
		$('.lastPage').click(function(){
			changePage($this.param.totalPage, $(this));
		});
		//跳转到指定页数
		$('#goToPage').click(function(){
			var goToPage = parseInt($('#pageNum').val());
			if(isNaN(goToPage))
				return false;
			if(goToPage > $this.param.totalPage){
				goToPage = $this.param.totalPage;
			}
			if(goToPage < 1){
				goToPage = 1;
			}
			changePage(goToPage, $(this));
		});
		$('#pageNum').keyup(function(event){   
     		//获取当前按键的键值   
     		//jQuery的event对象上有一个which的属性可以获得键盘按键的键值   
     		var keycode = event.which;   
    		//处理回车的情况   
     		if(keycode == 13){
     			$('#goToPage').click();
    		} 
 		});
	},
	//初始化组件
	init: function(control,action,param,callBack){
		//初始化参数
		this.param = $.extend({}, this.param, param);
		var $this = this;
		
		//如果组件初始化需要绑定UI
		if($('.pageControl').is(':empty')){
			this.bindUI();
			this.bindEvants(callBack);
		}
			
		//执行api
		if(this.param.needLayer)
			layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
		
		if(!Y.checkNull($this.param.alwaysCallBack))
				$this.param.alwaysCallBack();
		
		Y.API2(control, action, $this.param,function(data){
			if($this.param.needLayer)
				layer.closeAll('dialog');
			
			if(data.Success == false){
				swal(data.Message, '', 'error');
			}else{
				//存储结果
				$this.param.result = data;
				//获取总记录数
				$this.param.totalRecord = data.length;
				$('#recordCount').text($this.param.totalRecord);
				//计算总页数
				$this.param.totalPage = parseInt($this.param.totalRecord / $this.param.Size);
				if($this.param.totalRecord % $this.param.Size > 0)
					$this.param.totalPage += 1;
				$('#pageCount').text($this.param.totalPage);
			}
			$this.loadList(callBack);
		})
		
	}
}

//指定容器适用
var pageControlBindEl = {
	//加载列表
	loadList:function(params,callBack){
		var el = params.BindEl == '' ? '' : params.BindEl + ' ';
		var $this = this;
		if(params.needLayer)
			layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
		Y.API2(params.control, params.action, params,function(data){
			if(params.needLayer)
				layer.closeAll('dialog');
			
			if(!Y.checkNull(params.alwaysCallBack))
				params.alwaysCallBack();
			
			if(data.Success == false){
				swal(data.Message, '', 'error');
			}else{
				params.totalPage = data['页面总数'];
				$(el+'.pageCount').text(params.totalPage);
				params.totalRecord = data['记录总数'];
				$(el+'.recordCount').text(params.totalRecord);
				$(el+'.pageNum').val(params.Page);

				if (params.Page == 1) {
					$(el+'.firstPage').addClass('notUse');
					$(el+'.prevPage').addClass('notUse');
				} else {
					$(el+'.firstPage').removeClass('notUse');
					$(el+'.prevPage').removeClass('notUse');
				}

				if (params.Page >= params.totalPage) {
					$(el+'.lastPage').addClass('notUse');
					$(el+'.nextPage').addClass('notUse');
				} else {
					$(el+'.lastPage').removeClass('notUse');
					$(el+'.nextPage').removeClass('notUse');
				}
				
				callBack({
					page: params.Page,
					data: data
				});
			}
		})
	},
	//绑定ui
	bindUI: function(params){
		var el = params.BindEl == '' ? '' : params.BindEl + ' ';
		if($(el + '.pageControl').children().size() > 0)
			$(el + '.pageControl').empty();
		var html  = '';
			html += '<span class="recordCount">'+ params.totalRecord +'</span>'
			html += '<span class="pageCount">'+ params.totalPage +'</span>';
			html += '<input class="goToPage btn btn-primary" type="button" value="GO" />';
			html += '<i class="lastPage fa fa-step-forward"></i>';
			html += '<i class="nextPage fa fa-forward"></i>';
			html += '<input class="pageNum" class="form-control" type="number" value="'+ params.Page +'" />';
			html += '<i class="prevPage fa fa-backward"></i>';
			html += '<i class="firstPage fa fa-step-backward"></i>';
		$(el + '.pageControl').append(html);
	},
	//绑定事件
	bindEvants: function(params,callBack){
		var el = params.BindEl == '' ? '' : params.BindEl + ' ';
		//改变页数加载列表
		var $this = this;
		var changePage = function(pageNo,element){
			if(element.hasClass('notUse'))
				return false;
			params.Page = pageNo;
			$this.loadList(params,callBack);
		}
		//点击首页
		$(el+'.firstPage').click(function(){
			changePage(1, $(this));
		});
		//点击上一页
		$(el+'.prevPage').click(function(){
			changePage(params.Page - 1, $(this));
		});
		//点击下一页
		$(el+'.nextPage').click(function(){
			changePage(params.Page + 1, $(this));
		});
		//点击尾页
		$(el+'.lastPage').click(function(){
			changePage(params.totalPage, $(this));
		});
		//跳转到指定页数
		$(el+'.goToPage').click(function(){
			var goToPage = parseInt($(el+'.pageNum').val());
			if(isNaN(goToPage))
				return false;
			if(goToPage > params.totalPage){
				goToPage = params.totalPage;
			}
			if(goToPage < 1){
				goToPage = 1;
			}
			changePage(goToPage, $(this));
		});
		$(el+'.pageNum').keyup(function(event){   
     		//获取当前按键的键值   
     		//jQuery的event对象上有一个which的属性可以获得键盘按键的键值   
     		var keycode = event.which;   
    		//处理回车的情况   
     		if(keycode == 13){
     			$(el+'.goToPage').click();
    		} 
 		});
	},
	//初始化组件
	init: function(control,action,param,callBack){
		var params = {
			control: '',
			action: '',
			Page: 1,
			Size: 500,
			totalPage: 1,
			totalRecord: 0,
			needLayer: true,		//用于判断是否需要显示加载层
			alwaysCallBack: null,	//执行api以后，总是会执行的方法
			BindEl: '',				//如果不传的话，则默认el
		};
		//初始化参数
		params.control = control;
		params.action = action;
		params = $.extend({}, params, param);
		
		//如果组件初始化需要绑定UI
		if($('.pageControl').is(':empty')){
			this.bindUI(params);
			this.bindEvants(params,callBack);
		}
			
		//执行api
		this.loadList(params,callBack);
	}
}
//指定容器适用 分页信息在PageData里面
var pageControlBindElIn = {
    control: '',
    action: '',
    param: {
        Page: 1,
        Size: 500,
        totalPage: 1,
        totalRecord: 0,
        needLayer: true,		//用于判断是否需要显示加载层
        alwaysCallBack: null,	//执行api以后，总是会执行的方法
        BindEl: '',				//如果不传的话，则默认el
    },
    //加载列表
    loadList: function (callBack) {
        var el = this.param.BindEl == '' ? '' : this.param.BindEl + ' ';
        var $this = this;
        if ($this.param.needLayer)
            layer.msg('玩命加载中……', { icon: 16, shade: 0.5, time: 0, maxWidth: 200 });
        Y.API2($this.control, $this.action, $this.param, function (data) {
            if ($this.param.needLayer)
                layer.closeAll('dialog');

            if (!Y.checkNull($this.param.alwaysCallBack))
                $this.param.alwaysCallBack();

            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                if (!Y.checkNull(data['页面总数']))
                {
                    $this.param.totalPage = data['页面总数'];
                    $this.param.totalRecord = data['记录总数'];
                }
                else//分页信息在PageData里面
                {
                    $this.param.totalPage = data.PageData['页面总数'];
                    $this.param.totalRecord = data.PageData['记录总数'];
                }
                   
                $(el + '.pageCount').text($this.param.totalPage);
                $(el + '.recordCount').text($this.param.totalRecord);
                $(el + '.pageNum').val($this.param.Page);

                if ($this.param.Page == 1) {
                    $(el + '.firstPage').addClass('notUse');
                    $(el + '.prevPage').addClass('notUse');
                } else {
                    $(el + '.firstPage').removeClass('notUse');
                    $(el + '.prevPage').removeClass('notUse');
                }

                if ($this.param.Page >= $this.param.totalPage) {
                    $(el + '.lastPage').addClass('notUse');
                    $(el + '.nextPage').addClass('notUse');
                } else {
                    $(el + '.lastPage').removeClass('notUse');
                    $(el + '.nextPage').removeClass('notUse');
                }

                callBack({
                    page: $this.param.Page,
                    data: data
                });
            }
        })
    },
    //绑定ui
    bindUI: function () {
        var el = this.param.BindEl == '' ? '' : this.param.BindEl + ' ';
        if ($(el + '.pageControl').children().size() > 0)
            $(el + '.pageControl').empty();
        var html = '';
        html += '<span class="recordCount">' + this.param.totalRecord + '</span>'
        html += '<span class="pageCount">' + this.param.totalPage + '</span>';
        html += '<input class="goToPage btn btn-primary" type="button" value="GO" />';
        html += '<i class="lastPage fa fa-step-forward"></i>';
        html += '<i class="nextPage fa fa-forward"></i>';
        html += '<input class="pageNum" class="form-control" type="number" value="' + this.param.Page + '" />';
        html += '<i class="prevPage fa fa-backward"></i>';
        html += '<i class="firstPage fa fa-step-backward"></i>';
        $(el + '.pageControl').append(html);
    },
    //绑定事件
    bindEvants: function (callBack) {
        var el = this.param.BindEl == '' ? '' : this.param.BindEl + ' ';
        //改变页数加载列表
        var $this = this;
        var changePage = function (pageNo, element) {
            if (element.hasClass('notUse'))
                return false;
            $this.param.Page = pageNo;
            $this.loadList(callBack);
        }
        //点击首页
        $(el + '.firstPage').click(function () {
            changePage(1, $(this));
        });
        //点击上一页
        $(el + '.prevPage').click(function () {
            changePage($this.param.Page - 1, $(this));
        });
        //点击下一页
        $(el + '.nextPage').click(function () {
            changePage($this.param.Page + 1, $(this));
        });
        //点击尾页
        $(el + '.lastPage').click(function () {
            changePage($this.param.totalPage, $(this));
        });
        //跳转到指定页数
        $(el + '.goToPage').click(function () {
            var goToPage = parseInt($(el + '.pageNum').val());
            if (isNaN(goToPage))
                return false;
            if (goToPage > $this.param.totalPage) {
                goToPage = $this.param.totalPage;
            }
            if (goToPage < 1) {
                goToPage = 1;
            }
            changePage(goToPage, $(this));
        });
        $(el + '.pageNum').keyup(function (event) {
            //获取当前按键的键值   
            //jQuery的event对象上有一个which的属性可以获得键盘按键的键值   
            var keycode = event.which;
            //处理回车的情况   
            if (keycode == 13) {
                $(el + '.goToPage').click();
            }
        });
    },
    //初始化组件
    init: function (control, action, param, callBack) {
        //初始化参数
        this.control = control;
        this.action = action;
        this.param = $.extend({}, this.param, param);

        //如果组件初始化需要绑定UI
        if ($('.pageControl').is(':empty')) {
            this.bindUI();
            this.bindEvants(callBack);
        }

        //执行api
        this.loadList(callBack);
    }
}